import { PagedResultDto } from '@abp/ng.core';
import { Observable } from 'rxjs';
import { NzTableQueryParams } from 'ng-zorro-antd/table';
import { FilterPagedRequestDto } from '@proxy/dto';

export abstract class ListComponentBase<TDto> {
  total = 0;
  items: TDto[] = [];
  pageSize = 10;

  pageIndex = 1;
  filter = '';
  loading = false;

  ngOnInit(): void {
    this.loadData();
  }

  protected get request(): FilterPagedRequestDto {
    return {
      filter: this.filter,
      maxResultCount: this.pageSize,
      skipCount: this.pageSize * (this.pageIndex - 1)
    };
  }

  loadData() {
    this.loading = true;

    this.getList().subscribe((res: PagedResultDto<TDto>) => {
      this.loading = false;
      this.items = res.items;
      this.total = res.totalCount;
    });
  }

  abstract getList(): Observable<PagedResultDto<TDto>>;

  handlePage(query: NzTableQueryParams) {
    this.pageIndex = query.pageIndex;
    this.pageSize = query.pageSize;
    this.loadData();
  }

  refresh() {
    this.pageIndex = 1;
    this.loadData();
  }
}
